import { computed, Ref } from "vue";
import { getListApi, getPageStaticApi, batchApprovalApi, quotationApprovalExcelApi } from "@/api/waybillManagement/quotationApproval";
import { message, message as Message } from "ant-design-vue";
import CommonHook from "@/hook/commonHook";
export default function OperationHook(state: {
  searchForm: QuotationApproval.SearchForm;
  tableData: Ref<Array<QuotationApproval.ListRows>>;
  drawerOpts: Base.DrawerOpts;
  dialogOpts: Base.DialogOpts;
  loading: Ref<boolean>;
  tableRef: any;
  pageStat: Ref<Base.GeneraDataType>;
  activeKey: Ref<string>;
}) {
  const { searchForm, tableData, drawerOpts, loading, pageStat, activeKey, tableRef } = state;
  const selectRows = computed<Array<QuotationApproval.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const { getNoZeroKey } = CommonHook();
  const getData = async () => {
    const params: any = {
      ...getParams(),
      state: Number(activeKey.value)
    };
    loading.value = true;
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
          searchForm.beforePageNum = searchForm.pageNum;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const pageStateic = () => {
    const params = getParams();
    return getPageStaticApi({ ...params }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, {
          0: data[0],
          1: data[1],
          3: data[3]
        });
        activeKey.value = String(getNoZeroKey(pageStat.value, ["0", "1", "3"]));
      }
    });
  };
  const getParams = () => {
    const { waybillNums, customerName, accountPeriodIdList, customerIdList, pageNum, pageSize, beforePageNum, companyIdList } = searchForm;
    const params: Parameters<typeof getListApi>[0] = {
      state: 0,
      accountPeriodIdList,
      customerName,
      waybillNums: [],
      customerIdList,
      pageNum,
      pageSize,
      beforePageNum,
      companyIdList
    };
    if (waybillNums)
      params.waybillNums = waybillNums
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");

    if ((searchForm.orderByList as [])?.length) {
      params.orderByList = searchForm.orderByList;
    }

    return params;
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };
  const changeTabHandle = () => {
    getData();
  };
  // 审批
  const approveHandle = (row: QuotationApproval.ListRows) => {
    drawerOpts.title = `报价`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "Approve";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };
  const showHandle = (row: SingleOrderApproval.ListRows) => {
    drawerOpts.title = `报价详情`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "Approve";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  const batchExportHandle = () => {
    const params = {
      ...getParams(),
      state: Number(activeKey.value),
      list: selectRows.value.length ? selectRows.value : []
    };
    return quotationApprovalExcelApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("创建任务成功，请前往导出中心下载！");
      }
    });
  };

  const batchApproveHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    const params = {
      ids: selectRows.value.map((t: any) => t.orderShipmentQuotationId)
    };
    batchApprovalApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        Message.success("操作成功!");
        getData();
        pageStateic();
      }
    });
  };
  return {
    searchHandle,
    getData,
    showHandle,
    changeTabHandle,
    approveHandle,
    batchApproveHandle,
    batchExportHandle,
    getParams
  };
}
